<?php
/* 
 * ebiz front
 * 
 * templateReplaceUtil
 * 
 * @author allen
 * 
 */
class TemplateReplaceUtil 
{   
	public static function getTemplate($template){
		$config = $GLOBALS["config"];     //the value in the init file
//		$path = DIRECTORY_SEPARATOR.$config['template_name'].DIRECTORY_SEPARATOR.$template;
		$path = "/".$config['template_name']."/".$template;
		if(file_exists(TEMPLATEROOT.$path)){ 	//if file is exist
			$file = fopen(TEMPLATEROOT.$path, "r");
			$data = "";
			while(!feof($file)){
				$data .= fgets($file, 1024);
			}
			fclose($file);
			return $data;
		}
	}
	
	public static function getXmlTemplate($template){
	    $config = $GLOBALS["config"];     //the value in the init file
        $path = "/".$config['template_name']."/".$template;
        if(file_exists(TEMPLATEROOT.$path)){    //if file is exist
            $data = simplexml_load_file(TEMPLATEROOT.$path);
            return $data;
        }
        return false;		
	}
	
	
	public static function replaceTemplate($search, $replace, $str){	
		return str_replace($search, $replace, $str);
	}
	
	public static function getXmlParam($data){ 	//get the file of string model
		$arr = array();
		$xml = simplexml_load_string($data);
		if(!$xml){
			return;
		}
		foreach($xml->children() as $child){
			$tag = $child->getName().""; 	//get the tag name
			foreach($child->children() as $child){
				$param = $child->getName().""; 	//get the param name
				$value = $child.""; 	//get the attribute value
				$arr[$tag][$param] = $value;
			}
		}
		return $arr;
	}
	

//	public static function getXmlArr($data){ 	//get the string with xml model
//		$arr = array();
//		$reader = new XMLReader();
//		$reader->XML($data); 	//read the xml file
//		while ($reader->read()) { 	//read the line if have
//			if ($reader->hasAttributes) { 	//if have the attribute
//				$node=$reader->name;	//get the xml node name
//		      	while($reader->moveToNextAttribute()){	//if xml attribute not only one
//		      		$param = $reader->name;	 	//get the attribute name
//		      		$arr[$node][$param]= trim($reader->value); 	//get the attribute value put in the array
//		      	}
//			}
//		}	
//		$reader->close();
//		return $arr;
//	}
	
}


